//
//  LoginView.swift
//  FM
//
//  Created by x on 2017/11/7.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol LoginViewDelegate:NSObjectProtocol {
    
    /// 设置按钮点击事件
    ///
    /// - Parameter tag: 按钮的tag
    func setBtuDidClick(tag: Int)
}

class LoginView: UIView {
    
    weak var delegate:LoginViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    /// 初始化
    func setupUI() -> Void {
        
        backgroundColor = UIColor.white
        
        //1.添加子控件
        addSubview(phoneImage)
        addSubview(phoneTextFiled)
        addSubview(phoneLbale)
        addSubview(pwdImage)
        addSubview(pwdTextFiled)
        addSubview(pwdLbale)
        addSubview(loginBtu)
        addSubview(unregisterBtu)
        addSubview(forgetPwdBtu)
        addSubview(markLable)
        
        //2.布局子控件
        let dic = ["phoneImage":phoneImage,"phoneTextFiled":phoneTextFiled,"phoneLbale":phoneLbale,"pwdImage":pwdImage,"pwdTextFiled":pwdTextFiled,"pwdLbale":pwdLbale,"loginBtu":loginBtu,"unregisterBtu":unregisterBtu,"forgetPwdBtu":forgetPwdBtu,"markLable":markLable] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[phoneImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-40-[phoneImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[phoneImage]-10-[phoneTextFiled]-20-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[phoneLbale]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[phoneTextFiled]-20-[phoneLbale(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[pwdImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[phoneLbale]-20-[pwdImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[pwdImage]-10-[pwdTextFiled]-20-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[pwdLbale]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[pwdTextFiled]-20-[pwdLbale(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[loginBtu]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[pwdLbale]-100-[loginBtu(40)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markLable(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[loginBtu]-10-[markLable(15)]", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[unregisterBtu(80)]-5-[markLable]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[unregisterBtu(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markLable]-5-[forgetPwdBtu(80)]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[forgetPwdBtu(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        addConstraints(cons)
    }
    
    
    //MARK:-- 懒加载
    private lazy var phoneImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_phone")
    
    fileprivate lazy var phoneTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请输入手机号"
        textFiled.keyboardType = UIKeyboardType.phonePad
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    private lazy var phoneLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    private lazy var pwdImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_mima")
    
    fileprivate lazy var pwdTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请输入密码"
        textFiled.isSecureTextEntry = true
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    private lazy var pwdLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    private lazy var loginBtu:UIButton = {
        let btu = UIButton.buttonWith(title: "登入", titleColor: UIColor.black, fontSize: 16, tag: 1, target: self, action: #selector(btuClick(btu:)))
        btu.backgroundColor = UIColor.RGBColor(r: 204, g: 204, b: 204)
        btu.layer.cornerRadius = 20
        
        
        return btu
    }()
    
    private lazy var unregisterBtu:UIButton = UIButton.buttonWith(title: "尚未注册?", titleColor: UIColor.RGBColor(r: 231, g: 103, b: 61), fontSize: 14, tag: 2, target: self, action: #selector(btuClick(btu:)))
    
    private lazy var forgetPwdBtu:UIButton = UIButton.buttonWith(title: "忘记密码?", titleColor: UIColor.RGBColor(r: 231, g: 103, b: 61), fontSize: 14, tag: 3, target: self, action: #selector(btuClick(btu:)))
    
    private lazy var markLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 231, g: 103, b: 61))
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//MARK: -- 一些按钮点击事件

extension LoginView {
    /// 登入按钮点击事件
    func btuClick(btu:UIButton) -> Void {
        
        delegate?.setBtuDidClick(tag: btu.tag)
    }
}

//MARK: textFiled的一些时间

extension LoginView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        phoneTextFiled.resignFirstResponder()
        pwdTextFiled.resignFirstResponder()
    }
}
